import Ext from '@/common/web_extension';
import { getPlayTab, getPanelTab, getTab } from '../tab';

// 给panel发送消息
export const askPanel = async (cmd: string, args: any, timeout: number = 10000) => {
    const tab: any = await getPanelTab()

    return new Promise((resolve, reject) => {
        if (!tab) resolve(null)
        Ext.tabs.sendMessage(
            tab.id,
            {
                type: 'BG_ASK_PANEL',
                cmd,
                args,
                timeout
            },
            { frameId: 0 },
            (response: any) => {
                if (Ext.runtime.lastError) {
                    reject(Ext.runtime.lastError.message)
                } else {
                    console.log('askPanel response:>> ', response)
                    resolve(response)
                }
            })
    }).catch((e: any) => {
        console.log('askPanel error:>> ', e)
    })
}

// 给playTab发送消息
export const askPlayTab = async (cmd: string, args?: any, timeout?: number) => {
    const tab: any = await getPlayTab()
    return new Promise((resolve, reject) => {
        if (!tab) resolve(null)
        Ext.tabs.sendMessage(
            tab.id,
            {
                type: 'BG_ASK_CS',
                cmd,
                args,
                timeout
            },
            { frameId: 0 },
            (response: any) => {
                if (Ext.runtime.lastError) {
                    reject(Ext.runtime.lastError.message)
                } else {
                    console.log('askPlayTab response:>> ', response)
                    resolve(response)
                }
            })
    }).catch((e: any) => {
        console.log('askPlayTab error:>> ', e)
    })
}

export const askTab = async (tabId: number, cmd: string, args: any, timeout: number = 10000) => {
    const tab = await getTab(tabId)
    return new Promise((resolve, reject) => {
        if (!tab) resolve(null)
        Ext.tabs.sendMessage(
            tabId,
            {
                type: 'BG_ASK_CS',
                cmd,
                args,
                timeout
            },
            { frameId: 0 },
            (response: any) => {
                if (Ext.runtime.lastError) {
                    reject(Ext.runtime.lastError.message)
                } else {
                    console.log('askTab response:>> ', response)
                    resolve(response)
                }
            })
    }).catch((e: any) => {
        console.log('askTab error:>> ', e)
    })
}

// 给recordTab发送消息
export const askRecordTab = async (cmd: string, args: any, timeout: number = 10000) => {
    const tab: any = await getPlayTab()

    return new Promise((resolve, reject) => {
        if (!tab) resolve(null)
        Ext.tabs.sendMessage(
            tab.id,
            {
                type: 'BG_ASK_CS',
                cmd,
                args,
                timeout
            },
            { frameId: 0 },
            (response: any) => {
                if (Ext.runtime.lastError) {
                    reject(Ext.runtime.lastError.message)
                } else {
                    console.log('askRecordTab response:>> ', response)
                    resolve(response)
                }
            })
    }).catch((e: any) => {
        console.log('askRecordTab error:>> ', e)
    })
}

// 给service_worker发送消息
export const askBg = (cmd: string, args?: any, timeout?: number) => {
    return new Promise((resolve, reject) => {
        Ext.runtime.sendMessage(
            { type: 'CS_ASK_BG', cmd, args, timeout },
            (response: any) => {
                if (Ext.runtime.lastError) {
                    reject(Ext.runtime.lastError.message)
                } else {
                    console.log('askBg response:>> ', response)
                    resolve(response)
                }
            })
    }).catch((e: any) => {
        console.log('askBg error:>> ', e)
    })
}